<template>
  <div class="app-container">
    <content-card>
      <div class="filter-container">
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          @click="add"
        >
          新增
        </el-button>
      </div>
      <el-table
        ref="tree"
        v-loading="listLoading"
        element-loading-text="Loading"
        fit
        class="table-container"
        highlight-current-row
        :data="list"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
          fixed
          label="ID"
          width="120"
        >
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column
          label="权限名称"
        >
          <template slot-scope="scope">{{ scope.row.display_name }}</template>
        </el-table-column>
        <el-table-column
          label="权限标识"
        >
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column
          label="权限路径"
        >
          <template slot-scope="scope">{{ scope.row.effect_uri }}</template>
        </el-table-column>
        <el-table-column
          label="权限路径"
        >
          <template slot-scope="scope">{{ scope.row.description }}</template>
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="270"
        >
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="append(scope)"
              >
                新增
              </el-button>
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="edit(scope)"
              >
                修改
              </el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="del(scope)"
              >
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </content-card>
    <el-dialog
      :visible.sync="dialogVisibleAppend"
      :title="'新增'"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="tempp"
        label-width="120px"
        label-position="right"
      >
        <el-form-item label="权限名称" prop="display_name">
          <el-input v-model="tempp.display_name" placeholder="权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="name">
          <el-input v-model="tempp.name" placeholder="权限标识">
            <template v-if="temp.name" slot="prepend">{{ temp.name }}_</template>
          </el-input>
        </el-form-item>
        <el-form-item label="作用路由">
          <el-input v-model="tempp.effect_uri" type="textarea" placeholder="作用路由" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="tempp.description" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
      <div class="text-right">
        <el-button type="danger" @click="dialogVisibleAppend = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="'编辑'"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-width="120px"
        label-position="right"
      >
        <el-form-item label="权限名称" prop="display_name">
          <el-input v-model="temp.display_name" placeholder="权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="name">
          <el-input v-model="temp.name" placeholder="权限标识" />
        </el-form-item>
        <el-form-item label="作用路由">
          <el-input v-model="temp.effect_uri" type="textarea" placeholder="作用路由" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="temp.description" type="textarea" placeholder="备注" />
        </el-form-item>
      </el-form>
      <div class="text-right">
        <el-button type="danger" @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, store, del } from '@/api/permission'
import { deepClone } from '@/utils'
import ContentCard from '@/components/ContentCard'

const _temp = {
  id: '',
  parent_id: 0,
  effect_uri: '',
  name: '',
  display_name: '',
  description: ''
}
const _tempp = {
  display_name: '',
  name: '',
  effect_uri: '',
  parent_id: 0,
  description: ''
}

export default {
  components: {
    ContentCard
  },
  data() {
    return {
      filterText: '',
      list: [],
      // parents: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      listLoading: true,
      temp: Object.assign({}, _temp),
      dialogVisible: false,
      dialogVisibleAppend: false,
      dialogType: 'create',
      tempp: Object.assign({}, _tempp),
      name: '',
      loading: false,
      rules: {
        display_name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        this.list = response.data.list
        this.listLoading = false
      })
    },
    resetTemp() {
      this.name = ''
      this.temp = Object.assign({}, _temp)
    },
    add() {
      this.resetTemp()
      this.dialogVisibleAppend = true
      this.dialogType = 'create'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    append(scope) {
      this.resetTemp()
      this.dialogVisibleAppend = true
      this.dialogType = 'append'
      this.temp = deepClone(scope.row)
      this.name = this.temp.name
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    edit(scope) {
      this.resetTemp()
      this.dialogVisible = true
      this.dialogType = 'modify'
      this.temp = deepClone(scope.row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    del(scope) {
      this.$confirm('确认删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(scope.row.id).then(response => {
          setTimeout(() => {
            this.fetchData()
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          }, 300)
        })
      })
    },
    submit() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return
          }
          let obj
          if (this.dialogVisible) {
            obj = this.temp
          } else {
            this.tempp.parent_id = this.temp.id ? this.temp.id : 0
            if (this.name) {
              this.tempp.name = this.name + '_' + this.tempp.name
            }
            obj = this.tempp
          }
          this.loading = true
          store(obj).then(response => {
            setTimeout(() => {
              this.fetchData()
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.dialogVisibleAppend = false
              this.dialogVisible = false
              this.loading = false
            }, 300)
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
